import { HtmlNode, HtmlNodeModel, NodeConfig, GraphModel } from '@logicflow/core'
import { getBpmnId } from '../getBpmnId'
import './userTask.scss'
class ServiceTaskModel extends HtmlNodeModel {
  static extendKey = 'ServiceTaskModel'
  constructor(data: NodeConfig, graphModel: GraphModel) {
    if (!data.id) {
      data.id = `serviceTask_${getBpmnId()}`
    }
    if (!data.text) {
      data.text = ''
    }
    if (data.text && typeof data.text === 'string') {
      data.text = {
        value: data.text,
        x: data.x,
        y: data.y + 2,
      }
    }
    super(data, graphModel)
  }
  setAttributes(): void {
    this.width = 150
    this.height = 50
  }
  getTextStyle() {
    const style = super.getTextStyle()
    style.color = '#fff'
    style.fontSize = 16
    return style
  }
}

class ServiceTaskView extends HtmlNode {
  static extendKey = 'ServiceTaskModel'
  setHtml(rootEl: HTMLElement) {
    const { properties } = this.props.model
    const el = document.createElement('div')
    el.className = 'uml-wrapper'
    if (properties.complete) {
      el.className = 'uml-wrapper completed'
    }
    if (properties.current) {
      el.className = 'uml-wrapper current'
    }
    // 需要先把之前渲染的子节点清除掉。
    rootEl.innerHTML = ''
    rootEl.appendChild(el)
  }
}

const ServiceTask = {
  type: 'bpmn2:serviceTask',
  view: ServiceTaskView,
  model: ServiceTaskModel,
}

export { ServiceTaskView, ServiceTaskModel }
export default ServiceTask
